package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.Function;

public class CodeImage extends Inputsuper{

	private static final long serialVersionUID = 3929148800712450286L;
	private String  fontColor = "255, 0, 0";//字体颜色
	private Integer length = 4; //验证码长度
	private Integer cheight = 80;//高
	private Integer cwidth = 160;//宽
	private Integer cmaxSize=40;   //字体最大
	private Integer cminSize=20;//字体最小
	private Integer counter=null;//干绕方式
	private String  msg=null;
	private String  codename="noka_code";
	private String  imageStyle=null;//图片样式
	private String  imageClas=null;//图片样式
	private Integer kword=1;
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(potjs(request));
		stringBuilder.append(nkUtiljs(request));//加载nkjs
		String basePath = basepath(request);
		String prv = "?FontColor="+fontColor;
		prv+="&Length="+length;
		prv+="&Height="+cheight;
		prv+="&Width="+cwidth;
		prv+="&MaxSize="+cmaxSize;
		prv+="&MinSize="+cminSize;
		if(counter!=null)
			prv+="&counter="+counter;
		prv+="&codename="+codename;
		prv+="&kword="+kword;
		
		stringBuilder.append("<input type=\"text\" ");
		stringBuilder.append(getBaseStr());
		stringBuilder.append("/>\n");
		if(msg==null)
			msg=Function.lung(request,"org_nokatag_codeimage");
		if(imageStyle!=null && imageStyle.trim().length()>1)
			stringBuilder.append("<img onclick=\"ac_"+getId()+"()\" style= \""+imageStyle+"\" src=\""+basePath+"/nokatag/codeimg_"+getId()+".codeimg"+prv+"\" id=\"sn_"+getId()+"\"/><a  href=\"#\" onclick=\"ac_"+getId()+"()\">"+msg+"</a> \n");
		else if(imageClas!=null && imageClas.trim().length()>1)
			stringBuilder.append("<img onclick=\"ac_"+getId()+"()\" class= \""+imageClas+"\" src=\""+basePath+"/nokatag/codeimg_"+getId()+".codeimg"+prv+"\" id=\"sn_"+getId()+"\"/><a  href=\"#\" onclick=\"ac_"+getId()+"()\">"+msg+"</a> \n");
		else
			stringBuilder.append("<img onclick=\"ac_"+getId()+"()\" style= \"cursor:pointer;margin-right:0px;vertical-align:-"+(int)(cheight/2)+"px;\" src=\""+basePath+"/nokatag/codeimg_"+getId()+".codeimg"+prv+"\" id=\"sn_"+getId()+"\"/><a  href=\"#\" onclick=\"ac_"+getId()+"()\">"+msg+"</a> \n");
		stringBuilder.append("<script type=\"text/javascript\">\n");
		stringBuilder.append("function ac_"+getId()+"(){\n");
		stringBuilder.append("	$('sn_"+getId()+"').src=\""+basePath+"/nokatag/codeimg_"+getId()+".codeimg"+prv+"\"+\"&\"+new Date();\n");
		stringBuilder.append(" }\n");
		stringBuilder.append("Event.observe(document, 'dom:loaded', function() {");
		//----------------------------------
		stringBuilder.append("$('"+getId()+"').setDisabled = function(a){");
		stringBuilder.append("	$('"+getId()+"').writeAttribute({disabled:a});");
		stringBuilder.append("};");
		stringBuilder.append("$('"+getId()+"').setReadonly = function(a){");
		stringBuilder.append("	$('"+getId()+"').writeAttribute({readonly:a});");
		stringBuilder.append("};");
		stringBuilder.append("});");
		stringBuilder.append("</script>\n");
		
		try {
			jspOut.write(stringBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	public Integer getCheight() {
		return cheight;
	}
	public void setCheight(Integer cheight) {
		this.cheight = cheight;
	}
	public Integer getCmaxSize() {
		return cmaxSize;
	}
	public void setCmaxSize(Integer cmaxSize) {
		this.cmaxSize = cmaxSize;
	}
	public Integer getCminSize() {
		return cminSize;
	}
	public void setCminSize(Integer cminSize) {
		this.cminSize = cminSize;
	}
	public Integer getCounter() {
		return counter;
	}
	public void setCounter(Integer counter) {
		this.counter = counter;
	}
	public Integer getCwidth() {
		return cwidth;
	}
	public void setCwidth(Integer cwidth) {
		this.cwidth = cwidth;
	}
	public String getFontColor() {
		return fontColor;
	}
	public void setFontColor(String fontColor) {
		this.fontColor = fontColor;
	}
	public Integer getLength() {
		return length;
	}
	public void setLength(Integer length) {
		this.length = length;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getCodename() {
		return codename;
	}
	public void setCodename(String codename) {
		this.codename = codename;
	}
	public Integer getKword() {
		return kword;
	}
	public void setKword(Integer kword) {
		this.kword = kword;
	}
	public String getImageStyle() {
		return imageStyle;
	}
	public void setImageStyle(String imageStyle) {
		this.imageStyle = imageStyle;
	}
	public String getImageClas() {
		return imageClas;
	}
	public void setImageClas(String imageClas) {
		this.imageClas = imageClas;
	}
}
